package com.yanqu.road.dao.impl.union.cross;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.cross.CrossUnionFlag;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionFlagDaoImpl extends TempDataObjectDao<CrossUnionFlag>{

    public static String getUnionFlagSql = "select * from t_c_union_flag";
    public Map<String, Map<Integer, CrossUnionFlag>> getUnionFlag() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Integer, CrossUnionFlag>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionFlagSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionFlag unionFlag = parse(rs);
                String unionUid = unionFlag.getUnionUid();
                if (!dataMap.containsKey(unionUid)) {
                    dataMap.put(unionUid, new ConcurrentHashMap<>());
                }
                Map<Integer, CrossUnionFlag> flagMap = dataMap.get(unionUid);
                flagMap.put(unionFlag.getFlagId(), unionFlag);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUnionFlagSql = "update t_c_union_flag set expireTime=?,`read`=? where unionUid=? and flagId=?";
    @Override
    protected boolean updateData(CrossUnionFlag unionFlag) {
        unionFlag.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, unionFlag.getExpireTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, unionFlag.isRead()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionFlag.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, unionFlag.getFlagId()));
        if (getDb().executeNoneQuery(updateUnionFlagSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUnionFlagSql = "insert into t_c_union_flag(unionUid,flagId,expireTime,`read`) values (?,?,?,?)";
    @Override
    protected boolean addData(CrossUnionFlag unionFlag) {
        unionFlag.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, unionFlag.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, unionFlag.getFlagId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionFlag.getExpireTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, unionFlag.isRead()));
        if (getDb().executeNoneQuery(insertUnionFlagSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossUnionFlag parseData(ResultSet rs) throws SQLException {
        CrossUnionFlag unionFlag = new CrossUnionFlag();
        unionFlag.setUnionUid(rs.getString("unionUid"));
        unionFlag.setFlagId(rs.getInt("flagId"));
        unionFlag.setExpireTime(rs.getLong("expireTime"));
        unionFlag.setRead(rs.getBoolean("read"));
        return unionFlag;
    }

}
